import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../domain/entities/project.dart';

/// 项目状态
abstract class ProjectState {}

class ProjectInitial extends ProjectState {}

class ProjectLoading extends ProjectState {}

class ProjectsLoaded extends ProjectState {
  final List<Project> projects;
  ProjectsLoaded(this.projects);
}

class ProjectLoaded extends ProjectState {
  final Project project;
  ProjectLoaded(this.project);
}

class ProjectError extends ProjectState {
  final String message;
  ProjectError(this.message);
}

/// 项目状态通知器
class ProjectStateNotifier extends Notifier<ProjectState> {
  @override
  ProjectState build() {
    return ProjectInitial();
  }

  /// 获取项目列表
  Future<void> loadProjects() async {
    state = ProjectLoading();

    try {
      // 模拟数据
      final mockProjects = [
        Project(
          id: 1,
          projectName: '电商平台优化项目',
          description: '提升用户体验和系统性能',
          status: ProjectStatus.inProgress,
          priority: Priority.high,
          creatorName: '管理员',
          deadline: DateTime.now().add(const Duration(days: 60)),
          createTime: DateTime.now().subtract(const Duration(days: 30)),
          updateTime: DateTime.now().subtract(const Duration(days: 1)),
        ),
        Project(
          id: 2,
          projectName: '移动应用开发',
          description: '开发企业级移动应用',
          status: ProjectStatus.planning,
          priority: Priority.medium,
          creatorName: '项目经理',
          deadline: DateTime.now().add(const Duration(days: 90)),
          createTime: DateTime.now().subtract(const Duration(days: 10)),
          updateTime: DateTime.now().subtract(const Duration(days: 2)),
        ),
      ];

      state = ProjectsLoaded(mockProjects);
    } catch (e) {
      state = ProjectError(e.toString());
    }
  }

  /// 获取项目详情
  Future<void> loadProject(int projectId) async {
    state = ProjectLoading();

    try {
      final mockProject = Project(
        id: projectId,
        projectName: '项目 #$projectId',
        description: '这是项目 #$projectId 的详细描述',
        status: ProjectStatus.inProgress,
        priority: Priority.high,
        creatorName: '管理员',
        deadline: DateTime.now().add(const Duration(days: 45)),
        createTime: DateTime.now().subtract(const Duration(days: 15)),
        updateTime: DateTime.now().subtract(const Duration(days: 1)),
      );

      state = ProjectLoaded(mockProject);
    } catch (e) {
      state = ProjectError(e.toString());
    }
  }
}

/// 项目状态Provider
final projectStateProvider =
    NotifierProvider<ProjectStateNotifier, ProjectState>(
        ProjectStateNotifier.new);

/// 项目列表Provider
final projectsProvider = Provider<List<Project>>((ref) {
  final projectState = ref.watch(projectStateProvider);
  if (projectState is ProjectsLoaded) {
    return projectState.projects;
  }
  return [];
});

/// 当前项目Provider
final currentProjectProvider = Provider<Project?>((ref) {
  final projectState = ref.watch(projectStateProvider);
  if (projectState is ProjectLoaded) {
    return projectState.project;
  }
  return null;
});
